import { Component, OnInit, Input, Injector } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, ValidationErrors, Validators } from '@angular/forms';
import { Resume } from '../../domains/resume.domain';
import { ResumeMgeSvr } from '../../services/resume-mge-svr.service';
import { from, Observable, Observer } from 'rxjs';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzUploadChangeParam, NzUploadFile } from 'ng-zorro-antd/upload';
import { CoreComponent } from 'pengesoft-ng-lib';
import { NzModalRef } from 'ng-zorro-antd/modal';

@Component({
  selector: 'app-add-candidate',
  templateUrl: './add-candidate.component.html',
  styleUrls: ['./add-candidate.component.less']
})
export class AddCandidateComponent extends CoreComponent implements OnInit {
  validateForm: FormGroup;
  private file: NzUploadFile;
  data: any;
  page: any;
  infomation = new Resume();
  @Input() info: any;
  allJobs: any;
  listOfOption = [
    { label: '大专', value: '大专' },
    { label: '本科', value: '本科' },
    { label: '研究生', value: '研究生', }
  ];
  constructor(
    private injector: Injector,
    private fb: FormBuilder,
    private msg: NzMessageService,
    private modal: NzModalRef,
    private resumeSvr: ResumeMgeSvr) {
    super(injector);
  }
  ngOnInit(): void {
    console.log(this.allJobs);
    console.log(this.data);
    this.validateForm = this.fb.group({
      Name: [this.data.Resume.Name, [Validators.required]],
      Education: [this.data.Resume.Education, [Validators.required]],
      JobId: [this.data.Resume.JobId, [Validators.required]],
      InfoSource: [this.data.Resume.InfoSource],
      Tel: [this.data.Resume.Tel, [Validators.required]],
      Email: [this.data.Resume.Email, [Validators.email, Validators.required]],
      // tslint:disable-next-line: radix
      ApplyType: [String(this.data.Resume.ApplyType), [Validators.required]],
      // ResumeFile: ['', [Validators.required]]
    });
  }
  // tslint:disable-next-line:max-line-length
  submitForm(value: { InfoSource: string; Name: string; JobId: string; Tel: string; Email: string; Education: string; ApplyType: number; ResumeFile: 'D:xxx'; }): void {
    // tslint:disable-next-line:forin
    console.log('12');
    // tslint:disable-next-line: forin
    for (const key in this.validateForm.controls) {
      this.validateForm.controls[key].markAsDirty();
      this.validateForm.controls[key].updateValueAndValidity();
    }
    this.infomation.Name = value.Name;
    this.infomation.Education = value.Education;
    this.infomation.JobId = value.JobId;
    this.infomation.Tel = value.Tel;
    this.infomation.Email = value.Email;
    this.infomation.InfoSource = value.InfoSource;
    // tslint:disable-next-line: radix
    this.infomation.ApplyType = value.ApplyType;
    this.infomation.ResumeFile = value.ResumeFile;
    if (this.data.Resume.Id === 0) {
      console.log(this.infomation);
      this.resumeSvr.addResume(new Resume(this.infomation)).then(res => {
        console.log(res);
        this.modal.destroy(this.page);
      }, err => {
        this.nzMessageSvr.error(err.Msg);
      });
    } else {
      this.data.Resume.Name = value.Name;
      this.data.Resume.Education = value.Education;
      this.data.Resume.JobId = value.JobId;
      this.data.Resume.Tel = value.Tel;
      this.data.Resume.Email = value.Email;
      this.data.Resume.InfoSource = value.InfoSource;
      this.data.Resume.ApplyType = value.ApplyType;
      this.data.Resume.ResumeFile = value.ResumeFile;
      this.resumeSvr.updateResume(new Resume(this.data.Resume)).then(res => {
        console.log(this.page);
        this.modal.destroy(this.page);
      }, err => {
        this.nzMessageSvr.error(err.Msg);
      });
    }
  }
  resetForm(e: MouseEvent): void {
    e.preventDefault();
    this.validateForm.reset();
    // tslint:disable-next-line:forin
    for (const key in this.validateForm.controls) {
      this.validateForm.controls[key].markAsPristine();
      this.validateForm.controls[key].updateValueAndValidity();
    }
  }

  handleChange(info: NzUploadChangeParam): void {
    if (info.file.status !== 'uploading') {
      this.file = info.file;
    }
    if (info.file.status === 'done') {
      this.msg.success(`${info.file.name} file uploaded successfully`);
    } else if (info.file.status === 'error') {
      this.msg.error(`${info.file.name} file upload failed.`);
    }
  }
  submitFile(e: MouseEvent): void {
    e.preventDefault();
  }

  change(value): void {
    this.infomation.Education = value;
  }

  change2(value): void {
    this.infomation.JobId = value;
  }
}
